<?php

namespace App\Http\Controllers;

use App\Models\Reward;
use App\Models\UserWallet;
use App\Models\WechatUser;
use App\Jobs\SendRewardMsg;
use Illuminate\Http\Request;

class RewardController extends Controller
{
    function list() {
        $pn = request()->input('pn', config('api.page_num'));
        $recId = request()->input('rec_id', 0);
        $list = Reward::where('receive', $recId)
            ->select('id', 'send', 'money', 'created_at')
            ->with('sendUser:id,name')
            ->orderBy('created_at', 'desc')
            ->paginate($pn);
        return response()->json(['recode' => 1,
            'data' => ['last_page' => $list->lastPage()
                , 'page' => $list->currentPage()
                , 'list' => $list->items()]]);
    }

    public function listBySend()
    {
        $pn = request()->input('pn', config('api.page_num'));
        $sendId = request()->input('send_id', 0);
        $list = Reward::where('send', $sendId)
            ->select('id', 'receive', 'money', 'created_at')
            ->with('receiveUser:id,name')
            ->orderBy('created_at', 'desc')
            ->paginate($pn);
        return response()->json(['recode' => 1,
            'data' => ['last_page' => $list->lastPage()
                , 'page' => $list->currentPage()
                , 'list' => $list->items()]]);
    }

    public function info()
    {
        $id = request()->input('id', 0);
        $info = Reward::find($id);
        if ($info) {
            return response()->json(['recode' => 1,
                'info' => [
                    'id' => $info->id,
                    'created_at' => $info->created_at,
                    'send_name' => $info->sendUser->name,
                    'send_nickname' => $info->sendUser->nickname,
                    'send_head_img' => $info->sendUser->head_img,
                    'receive_name' => $info->receiveUser->name,
                    'receive_nickname' => $info->receiveUser->nickname,
                    'receive_head_img' => $info->receiveUser->head_img,
                    'money' => $info->money,
                    'created_at' => $info->created_at,
                    'desc' => '感谢大侠投币',
                ],
            ]);
        } else {
            return response()->json(['recode' => 0,
                'msg' => '没找到记录']);
        }
    }

    public function reward()
    {
        $send = request()->input('send', 0);
        $receive = request()->input('reveive', 0);
        $money = request()->input('money', 0);

        $sendWallet = UserWallet::where('user_id', $send)->first();
        if ($sendWallet) {
            if ($sendWallet->money < $money) {
                return response()->json(['recode' => 0,
                    'msg' => '金额不足，赶紧PK赢红包吧']);
            }
        } else {
            return response()->json(['recode' => 0,
                'msg' => '金额不足，赶紧PK赢红包吧']);
        }

        $receiveWallet = WechatUser::find($receive);
        if (!$receiveWallet) {
            return response()->json(['recode' => 0,
                'msg' => '没找到接收者']);
        }

        $receiveWallet = UserWallet::firstOrNew(
            ['user_id' => $receive], ['money' => 0]
        );

        $reward = new Reward;
        $reward->send = $send;
        $reward->receive = $receive;
        $reward->money = $money;

        \DB::transaction(function () use ($reward, $sendWallet, $receiveWallet, $money) {
            $sendWallet->money = $sendWallet->money - $money;
            $receiveWallet->money = $receiveWallet->money + $money;

            $sendWallet->save();
            $receiveWallet->save();
            $reward->save();
        });

        $this->dispatch((new SendRewardMsg($reward,$receiveWallet->money)));

        return response()->json(['recode' => 1,
            'id' => $reward->id,
            'msg' => '打赏成功']);

    }
}
